package com.wzcl.app.controller;

import com.wzcl.app.dao.model.Grade;
import com.wzcl.app.dao.model.Requisition;
import com.wzcl.app.dao.model.Subject;
import com.wzcl.app.model.common.MyPage;
import com.wzcl.app.model.common.RVO;
import com.wzcl.app.model.dto.BaseDto;
import com.wzcl.app.model.dto.GradeDto;
import com.wzcl.app.model.dto.RequisitionDto;
import com.wzcl.app.model.dto.SubjectDto;
import com.wzcl.app.model.vo.GradeVo;
import com.wzcl.app.model.vo.RequisitionVo;
import com.wzcl.app.model.vo.SubjectVo;
import com.wzcl.app.service.GradeService;
import com.wzcl.app.service.HomeMassageService;
import com.wzcl.app.service.RequisitionService;
import com.wzcl.app.service.SubjectService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.log4j.Log4j2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;


@RestController
@RequestMapping("/index")
@Api(value = "首页controller", tags = {"首页相关接口"})
@Log4j2
public class IndexController{

    @Autowired
    private GradeService gradeService;

    @Autowired
    private SubjectService subjectService;

    @Autowired
    private HomeMassageService homeMassageService;

    @Autowired
    private RequisitionService requisitionService;

    @ApiOperation(value = "首页科目")
    @PostMapping("/getSubject")
    public RVO<List<SubjectVo>> SubjectData(){
        List<Subject> subjects = subjectService.getSubjectList(new SubjectDto());
        if(subjects!=null && subjects.size()>0){
            Subject subject = new Subject();
            subject.setId(-1);
            subject.setCode("Recommend");
            subject.setName("推荐");
            subjects.add(0,subject);
            List<SubjectVo> collect = subjects.stream().map(SubjectVo::create).collect(Collectors.toList());
            return RVO.success(collect);
        }
        return RVO.success(null);
    }

    @ApiOperation(value = "首页年级类型及年级数据")
    @PostMapping("/getGrade")
    public RVO<List<GradeVo>> gradeData(){
        List<Grade> gradeList = gradeService.getGradeList(new GradeDto());
        if(gradeList!=null && gradeList.size()>0){
            List<GradeVo> collect = gradeList.stream().map(GradeVo::create).collect(Collectors.toList());
            return RVO.success(collect);
        }
        return RVO.success(null);
    }


    @ApiOperation("获取申请信息")
    @PostMapping("/getRequisition")
    public RVO<List<RequisitionVo>> getRequisition(){
        RequisitionDto requisitionDto = new RequisitionDto();
        Map<String,Integer> orders = new HashMap<>();
        orders.put("createTime", BaseDto.DESC);
        requisitionDto.setOrders(orders);
        requisitionDto.setPageSize(20);
        MyPage<Requisition> page = requisitionService.getRequisitionPage(requisitionDto);
        if(page != null && page.getContent().size() >0){
            List<RequisitionVo> collect = page.getContent().stream().map(RequisitionVo::create).collect(Collectors.toList());
            return RVO.success(collect);
        }
        return RVO.success(null);
    }

}
